/*
 * $Id: CompoundValidator.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.support.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * The class {@code CompoundValidator} represents the class for compound validation.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 42 $
 */
public class CompoundValidator implements Validator {
    /**
     * The validators.
     */
    private final Validator[] validators;

    /**
     * The constructor.
     * Initializes the compound validator.
     *
     * @param validators The validators.
     */
    public CompoundValidator(Validator... validators) {
        this.validators = validators;
    }

    /**
     * Will return true if this class is in the specified list.
     *
     * @see org.springframework.validation.Validator#supports(Class)
     */
	@Override
    public boolean supports(Class<?> clazz) {
        for (Validator v : validators) {
            if (v.supports(clazz)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Validate the specified object using the validator registered for the object's class.
     *
     * @see org.springframework.validation.Validator#validate(Object, org.springframework.validation.Errors)
     */
	@Override
    public void validate(Object target, Errors errors) {
        for (Validator v : validators) {
            if (v.supports(target.getClass())) {
                v.validate(target, errors);
                break;
            }
        }
    }
}
